package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.Dto.NursingPlanDto;
import com.zzyl.serve.Vo.NursingPlanVo;
import com.zzyl.serve.Vo.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-04
 */
@Service
@Transactional
public class NursingPlanServiceImpl  extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //根据ID查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        System.out.println(nursingPlan+"==Plan=====================VO="+nursingPlanVo);

        //查询护理计划id查询关联的所有项目计划
         List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        System.out.println(list);
         nursingPlanVo.setProjectPlans(list);
         return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlan)
    {
        NursingPlan nursingPlan1 = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan,nursingPlan1);
        nursingPlan1.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan1);

        int count =nursingProjectPlanMapper.batchInsert(nursingPlan.getProjectPlans(),nursingPlan1.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlan)
    {
        try {
            //属性拷贝
            NursingPlan nursingPlan1 = new NursingPlan();
            BeanUtils.copyProperties(nursingPlan,nursingPlan1);

            //首先判断DTO中的项目列表是否为空，如果不为空，则先删除护理计划和护理项目的关系，然后重新批量添加
            if(nursingPlan.getProjectPlans() != null && !nursingPlan.getProjectPlans().isEmpty()){
                //删除护理计划和护理项目的关系
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan1.getId());
                //批量添加护理计划和护理项目的关系
                nursingProjectPlanMapper.batchInsert(nursingPlan.getProjectPlans(),nursingPlan1.getId());
            }
            return nursingPlanMapper.updateNursingPlan(nursingPlan1);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}
